package com.xxgc.ssm.controller;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xxgc.ssm.po.info.Info;
import com.xxgc.ssm.po.req.UserLoginReg;
import com.xxgc.ssm.service.IUserLogin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/login")
@Api(tags = "用户注册登录",description = "注册登录接口")
public class UserLogin {
    //搭建一个通道
    @Autowired
    private IUserLogin uls;
    @Autowired
    private Info info;

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户注册",notes = "用户名和手机号注册")
    public Info register(@RequestBody @ApiParam("用户对象") @Valid UserLoginReg.RegisterBean user, BindingResult result){
        // 如果有异常信息
        if (result.hasErrors()) {
            // 将异常信息输出
            info.setCode(-201);
            info.setMsg(result.getAllErrors().get(0).getDefaultMessage());
            return info;
        }else{
            return uls.insertRegister(user);
        }
    }

    @RequestMapping(value = "/loginByPhone",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户登录",notes = "使用手机号和密码登录")
    public Info loginByPhone(@RequestBody @ApiParam("用户对象") @Valid UserLoginReg.LoginBean user,
                             BindingResult result,
                             @ApiIgnore HttpSession session){
// 如果有异常信息
        if (result.hasErrors()) {
            // 将异常信息输出
            info.setCode(-201);
            info.setMsg(result.getAllErrors().get(0).getDefaultMessage());
            return info;
        }else{
            return uls.selectLoginByPhone(user,session);
        }
    }
}
